<?php

namespace App\Admin\Forms;

use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class WechatConfig extends Form
{
    /**
     * The form title.
     *
     * @var string
     */
    public $title = '小程序配置';

    protected $id = 1;

    /**
     * Handle the form request.
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request)
    {
        //dump($request->all());
        $param = $request->all();
        $env = $param['env'];
        \App\Models\Tool\WechatConfig::query()->where('id', $this->id)->update([
            'env' => $env,
        ]);
        Redis::connection()->set("code:version", $env);
        admin_success('操作成功');

        return back();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $states = [
            'on'  => ['value' => "product", 'text' => '生产', 'color' => 'success'],
            'off' => ['value' => "audit", 'text' => '审核', 'color' => 'danger'],
        ];
        $this->switch('env', __("环境变量"))->options($states)->default('product');
    }

    /**
     * The data of the form.
     *
     * @return array $data
     */
    public function data()
    {
        $data = \App\Models\Tool\WechatConfig::query()->where('id', $this->id)->first();
        return [
            'env' => $data->env,
        ];
    }
}
